import os, random

def Count (day: int, week: int, month: int, year: int): # 时间计算器
    '''
    day 日\n
    week 周\n
    month 月\n
    year 年\n
    '''
    
    n = year - 2000 # 计算距离2000年有多少年
    if (n % 4 == 0): #计算是否为闰年(只计算是否能被4整除)
        leap = 1 # 是为1
    else :
        leap = 0 # 否为0
    
    if (month == 12 and day == 13): # 年末计算
        year += 1
        month = 1
        day = 1
    elif ((month in [1,3,5,7,8,10] and day == 13) or
            (month in [4,6,9,11] and day == 12) or
            (month == 2 and day == 14 and leap == 0) or
            (month == 2 and day == 15 and leap == 1)): # 月末计算
        month += 1
        day = 1
    else :
        day += 1
        
    if (week == 7): # 周日则下一天为周一
        week = 1
    else :
        week += 1 # 不然就加一天
        
    seventh_shops_discount = random.randint (0,15) # 第七个超市打折随机数生成(只有为0时打折)
    
    return day, week, month, year, seventh_shops_discount # 返回日期
# ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
# 信息列表
def Menu (day: int, week: int, month: int, year: int, discount_num: list):
    '''
    day 日\n
    week 周\n
    month 月\n
    year 年\n
    '''
    
    if (week == 1): # 星期数字转换为汉语
        week_CH = '一'
    elif (week == 2):
        week_CH = '二'
    elif (week == 3):
        week_CH = '三'
    elif (week == 4):
        week_CH = '四'
    elif (week == 5):
        week_CH = '五'
    elif (week == 6):
        week_CH = '六'
    elif (week == 7):
        week_CH = '日'
        
    if (month in [1,2,3]): # 季节转换
        season = '春季'
    elif (month in [4,5,6]):
        season = '夏季'
    elif (month in [7,8,9]):
        season = '秋季'
    elif (month in [10,11,12]):
        season = '冬季'
    
    os.system ('cls')
    while (True):
        try :
            print ("\033[39;33m|---------------------------------------")
            print ("\033[39;33m|\033[0m今日日期：")
            print (f"\033[39;33m|\033[38;5;33m{year}\033[0m年\033[38;5;33m{month}\033[0m月\033[38;5;33m{day}\033[0m日")
            print (f"\033[39;33m|\033[0m星期\033[38;5;33m{week_CH}")
            print (f"\033[39;33m|\033[38;5;98m{season}")
            print ("\033[39;33m|---------------------------------------")
            if (len (discount_num) > 0): # 判断是否有打折数
                Discount (discount_num)
            else :
                print ("\033[39;33m|\033[0m今日无打折市场")
            print ("\033[39;33m|---------------------------------------")
            print ("\033[39;33m|\033[38;5;44m1. 万年历查询")
            print ("\033[39;33m|\033[38;5;44m2. 游戏日期说明")
            print ("\033[39;33m|\033[38;5;196m0. 返回")
            print ("\033[39;33m|---------------------------------------\033[0m")
            x = int (input ("输入需要的功能："))
            if (x == 1):
                os.system ('cls')
                PerpetualCalendar (year) # 万年历
            elif (x == 2):
                os.system ('cls')
                CalendarExplain ()
            elif (x == 0):
                os.system ('cls')
                return None
            else :
                raise ValueError
        except ValueError:
            os.system ('cls')
            print ("\033[1;31m无效选项！\033[0m\n")
# ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————        
def DiscountNum (day: int, week: int, month: int, seventh_shops_discount: int): # 打折数列表生成函数
    discount = [] # 重置打折数列表
    if (day == 1):
        discount.append (1)
        
    if (month in [2,4,6,8,10,12] and day == 1):
        discount.append (2)
        
    if (month in [1,3,5,7,9,11] and day == 12):
        discount.append (3)
        
    if (week == 3):
        discount.append (4)
        
    if ((day + month) % 7 == 0):
        discount.append (5)
    
    if (month == 11 and day == 11):
        discount.append (6)
    
    if (seventh_shops_discount == 0):
        discount.append (7)
    
    if (month in [4,5,6,7,8,9]):
        discount.append (8)
        
    if (month == 1 and day in [1,2,3,4,5]):
        discount.append (9)
    
    return discount
# ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————        
def Discount (discount_num): # 打折日输出相对应打折市场函数
    if (1 in discount_num):
        print ("\033[39;33m|\033[38;5;205m<五姨菜市场>折扣日！")
        
    if (2 in discount_num):
        print ("\033[39;33m|\033[38;5;205m<二狗肉市>折扣日！")
        
    if (3 in discount_num):
        print ("\033[39;33m|\033[38;5;205m<三叔五金行>折扣日！")
        
    if (4 in discount_num):
        print ("\033[39;33m|\033[38;5;205m<四姐药铺>折扣日！")
        
    if (5 in discount_num):
        print ("\033[39;33m|\033[38;5;205m<六兄布行>折扣日！")
    
    if (6 in discount_num):
        print ("\033[39;33m|\033[38;5;205m<大舅家具城>折扣日！")
    
    if (7 in discount_num):
        print ("\033[39;33m|\033[38;5;205m<十子杂货铺>折扣日！")
    
    if (8 in discount_num):
        print ("\033[39;33m|\033[38;5;205m<八婶水果店>折扣日！")
        
    if (9 in discount_num):
        print ("\033[39;33m|\033[38;5;205m<七哥电子城>折扣日！")
        
    return None
# ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
def CalendarExplain (): # 游戏特殊的日期记录说明
    print ("\033[39;33m|---------------------------------------")
    print ("\033[39;33m|\033[39;36m可以整除4的年份为闰年")
    print ("\033[39;33m|\033[39;36m一、三、五、七、八、十、十二月 十三天")
    print ("\033[39;33m|\033[39;36m四、六、九、十一月 十二天")
    print ("\033[39;33m|\033[39;36m二月 平年十三天，闰年十四天")
    print ("\033[39;33m|\033[39;36m每周七天")
    print ("\033[39;33m|---------------------------------------\033[0m")
    input ("按Enter键返回...")
    os.system ('cls')
    return None
# ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
def MonDay (x, week): # 日期(日)输出函数
    if (x < 10): # 单位日输出格式
        print (f" {x} ", end = '')
    elif (x >= 10): # 双位日输出格式
        print (f" {x}", end = '')
    if (week == 7): # 周日则换行重置周数
        print ()
        week = 1
    else : # 其他周数则空格然后周数加一
        print (" ", end = '')
        week += 1
    return week
# ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
def PerpetualCalendar (year): # 万年历主函数
    while (True):
        try :
            print ("\033[39;33m|---------------------------------------")
            print ("\033[39;33m|\033[39;31m注意事项：")
            print ("\033[39;33m|\033[39;95m只能查询2000年-9999年之间的年份")
            print ("\033[39;33m|\033[39;95m只能查询当前年份前后20年日历")
            if (year - 20 <= 2000): # 用于输出这次可以查询几几年到几几年
                print (f"\033[39;33m|\033[39;95m本次可查询\033[39;92m2000年-{year + 20}年")
            elif (year + 20 >= 9999):
                print (f"\033[39;33m|\033[39;95m本次可查询\033[39;92m{year - 20}年-9999年")
            else :
                print (f"\033[39;33m|\033[39;95m本次可查询\033[39;92m{year - 20}-{year + 20}年")
            print ("\033[39;33m|\033[39;95m游戏有特殊的时间记法，请提前知悉")
            print ("\033[39;33m|\033[39;96m输入0退出查询")
            print ("\033[39;33m|---------------------------------------\033[0m")
            input_year = int (input ("请输入查阅的年份："))
            os.system ('cls')
            if ((input_year < 2000 or input_year > 9999 or 
                input_year > year + 20 or input_year < year - 20) and (input_year != 0)): # 判断查询年份是否满足要求
                raise Exception
            elif (input_year >= 2000 and input_year <= 9999 and 
                input_year <= year + 20 and input_year >= year - 20):
                n = input_year - 2000 # 计算与2000年的差值
                mod = n % 28 # 计算27个周年的余数（28是循环结束+1，以保证mod可以在0-27之间循环）
                mod_to_x = {0:1, 1:1, 10:1, 19:1,
                            8:2, 9:2, 18:2, 27:2,
                            7:3, 16:3, 17:3, 26:3,
                            6:4, 15:4, 24:4, 25:4,
                            4:5, 5:5, 14:5, 23:5,
                            3:6, 12:6, 13:6, 22:6,
                            2:7, 11:7, 20:7, 21:7} # 对应差值年份的周数字典
                x = mod_to_x[mod] # 将字典里的对应周数放入x里（当年1月1日对应的周数） 
                print (f"\033[38;5;33m{input_year}\033[0m年") # 输出查询的年份
                if (input_year % 4 == 0): # 平闰年判断
                    print ("\033[38;5;77m闰年\n")
                else :
                    print ("\033[38;5;77m平年\n")
                month_ZH = {1: "一月", 2: '二月', 3: '三月', 
                            4: '四月', 5: '五月', 6: '六月', 
                            7: '七月', 8: '八月', 9: '九月', 
                            10: "十月", 11: "十一月", 12: "十二月"} # 月份字典
                for i in range (1,13): # 从1月份循环输出至12月份，13不会输出
                    print ("\033[39;94m", month_ZH [i])
                    print ("\033[39;33m---------------------------")
                    print ("\033[39;36mMon Tue Wed Thu Fri Sat Sun\033[0m")
                    if (x != 1): # 若当月第一天不为周一，则输出对应周数-1个空格来调整排版
                        for n in range (x-1) :
                            print ("    ", end = '')
                    if (i in [1, 3, 5, 7, 8, 10, 12]): # 13天输出
                        for j in range (1, 14):
                            x = MonDay (j, x)
                    elif (i in [4, 6, 9, 11]): # 12天输出
                        for j in range (1, 13):
                            x = MonDay (j, x)
                    elif (i == 2 and input_year % 4 ==0): # 闰年2月15天输出
                        for j in range (1, 16):
                            x = MonDay (j, x)
                    elif (i == 2 and input_year % 4 !=0): # 平年2月14天输出
                        for j in range (1, 15):
                            x = MonDay (j, x)
                    if (x != 1):
                        print ("\033[39;33m\n---------------------------\n")
                    else :
                        print ("\033[39;33m---------------------------\n")
                input ("\033[0m请在确认后按Enter键返回...")
                os.system ('cls')
            elif (input_year == 0):
                os.system ('cls')
                return None
        except ValueError:
            os.system ('cls')
            print ("\033[1;31m无效查询！\033[0m\n")
        except Exception:
            os.system ('cls')
            print ("\033[1;31m超出可查询范围！\033[0m\n")

# 调试          
# day = 1
# week = 1
# month = 1
# year = int (input ("year="))
# Menu (day, week, month, year)